import { WinBase } from "../../wins/WinBase";
import { globalType } from "../globalType";
import { ccs } from "../services";
import { TagsCtrl } from "../tags/tagsCtrl";

const {ccclass, property, menu} = cc._decorator;

@ccclass('PanelInfo')
export class PanelInfo {
    @property()
    key:string = '';

    @property(cc.Prefab)
    pfb:cc.Prefab = null;

    @property(cc.SpriteFrame)
    bgSpFrame:cc.SpriteFrame = null;

    node:cc.Node = null;
}
@ccclass
@menu('WinScript/BagWin')
export default class BagWin extends WinBase {

    @property([PanelInfo])
    panelArray: PanelInfo[] = [];

    @property(cc.Sprite)
    panelBgSprite: cc.Sprite = null;

    @property(TagsCtrl)
    tagsCtrl:TagsCtrl = null;

    private _showPanels: globalType.KeyObject<PanelInfo> = {}; //存不同的面板

    private _selectTagKey:string = null; //当前选中的
    
    private _tipDt:number = 1;

    //private _bgRect:cc.Rect = null;

    onLoad() {
        for(let i = 0, len = this.panelArray.length; i < len; i++) {
            let panel = this.panelArray[i];
            this._showPanels[panel.key] = panel;
        }
    }

    onInit() {
        
    }

    start () {
        this.tagsCtrl.setShowTip(true);
        //this.tagsCtrl.tagNodes[2].active = false;
        // this.tagsCtrl.selectIdx = this._args.tab || 0;
        // NodeEx.startNext(this.node, ()=>{
        //     if(this.panelBgSprite.node.parent && this.panelBgSprite.isValid) {
        //         this._bgRect = NodeEx.getNodeWorldRect(this.panelBgSprite.node);
        //     }
        // });
    }


    onSelectKey(key:string) {
        this.tagsCtrl.selectKey(key);
    }

    onTagClick(key:string) {
        //console.log('onTagClick', key);
        if(this._selectTagKey) {
            this._showPanels[this._selectTagKey].node.active = false;
        }
        this._selectTagKey = key;
        let panelInfo = this._showPanels[this._selectTagKey];
        if(panelInfo) {
            if(!panelInfo.node) {
                panelInfo.node = cc.instantiate(panelInfo.pfb);
                this.node.addChild(panelInfo.node);
            }
            this.panelBgSprite.spriteFrame = panelInfo.bgSpFrame;
            panelInfo.node.active = true;
            this.updatePanel();
        }
    }

    updatePanel(effectType:number = 0) {
        let panelInfo = this._showPanels[this._selectTagKey];
        // if(panelInfo && panelInfo.node) {
        //     switch(this._selectTagKey) {
        //         case 'bag':
        //             panelInfo.node.getComponent(BagCtrl).updateItems();
        //             break;
        //             case 'role':
        //                 panelInfo.node.getComponent(RoleCtrl).updateInfo();
        //                 break;
        //                 case 'swordtomb':
        //                     panelInfo.node.getComponent(SwordtombCtrl).updateInfo(true, effectType);
        //                     break;
        //                     case 'lifestyle':
        //                         panelInfo.node.getComponent(LifeStyleInfo).updateInfo();
        //                         break;
        //     }
        // }
        
    }

    getCtrl<T>(key:string):T {
        let panelInfo = this._showPanels[key];
        // if(panelInfo && panelInfo.node) {
        //     switch(key) {
        //         case 'bag':
        //             return panelInfo.node.getComponent(BagCtrl) as any;
        //             case 'role':
        //                 return panelInfo.node.getComponent(RoleCtrl) as any;;
        //                 case 'swordtomb':
        //                     return panelInfo.node.getComponent(SwordtombCtrl) as any;;
        //                     case 'lifestyle':
        //                         return panelInfo.node.getComponent(LifeStyleInfo) as any;
        //     }
        // }
        return null;
    }

    update (dt:number) {
        // this._tipDt += dt;
        // if(this._tipDt > 1) {
        //     this._tipDt = 0;
        //     let tipNodes = this.tagsCtrl.getTipNodes();
        //     if(tipNodes[0]) {
        //         sd.pf.updateTipSprite(tipNodes[0], BagM.instance().isTip(), 0, -10, 30);
        //     }
        // }
    }
}
